home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
Turnbull China Bikeride
/
Turnbull China Bikeride - Disc 2.iso
/
STUTTGART
/
TEMP
/
GNU
/
bison
/
SemanticAc
< prev
next >
Wrap
Text File
|
1995-06-28
|
1KB
|
34 lines
Semantic Actions
Previous: <Semantic Values=>SemanticVa> * Next: <Bison Parser=>BisonParse> * Up: <Concepts=>Concepts>
#Wrap on
{fH3}Semantic Actions{f}
In order to be useful, a program must do more than parse input; it must
also produce some output based on the input. In a Bison grammar, a grammar
rule can have an {fUnderline}action{f} made up of C statements. Each time the
parser recognizes a match for that rule, the action is executed.
\*Note <Actions=>Actions>.
Most of the time, the purpose of an action is to compute the semantic value
of the whole construct from the semantic values of its parts. For example,
suppose we have a rule which says an expression can be the sum of two
expressions. When the parser recognizes such a sum, each of the
subexpressions has a semantic value which describes how it was built up.
The action for this rule should create a similar sort of value for the
newly recognized larger expression.
For example, here is a rule that says an expression can be the sum of
two subexpressions:
#Wrap off
#fCode
expr: expr '+' expr \{ $$ = $1 + $3; \}
;
#f
#Wrap on
The action says how to produce the semantic value of the sum expression
from the values of the two subexpressions.